package com.dayaokeji.rhythmschoolstudent.client.message;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.Toolbar;

import com.beetle.bauhinia.gallery.GalleryImage;
import com.dayaokeji.imkitwrapper.fragment.GalleryGridFragment;
import com.dayaokeji.rhythmschoolstudent.R;
import com.dayaokeji.rhythmschoolstudent.client.common.base.common.BaseActivity;

import java.util.ArrayList;

import butterknife.BindView;

public class GalleryGridActivity extends BaseActivity {

    private static final String GALLERY_GRID_TAG = "gallery_grid";

    private static final String EXTRA_GALLERY_IMAGES = "gallery_images";
    private static final String EXTRA_IMG_POSITION = "img_position";

    @BindView(R.id.toolbar)
    Toolbar toolbar;
    private Fragment galleryGridFragment;
    private ArrayList<GalleryImage> galleryImages;
    private int position;


    public static void navigation(Context context, ArrayList<GalleryImage> imagesList, int position) {
        Intent intent = new Intent(context, GalleryGridActivity.class);
        intent.putParcelableArrayListExtra(EXTRA_GALLERY_IMAGES, imagesList);
        intent.putExtra(EXTRA_IMG_POSITION, position);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_gallery_grid);
        setSupportActionBar(toolbar);
        init();
    }


    private void init() {
        Intent intent = getIntent();
        galleryImages = intent.getParcelableArrayListExtra(EXTRA_GALLERY_IMAGES);
        position = intent.getIntExtra(EXTRA_IMG_POSITION, 0);

        setupView();
    }

    private void setupView() {
        galleryGridFragment = getSupportFragmentManager().findFragmentByTag(GALLERY_GRID_TAG);
        if (galleryGridFragment == null) {
            galleryGridFragment = GalleryGridFragment.newInstance(galleryImages, position);
        }
        getSupportFragmentManager().beginTransaction().replace(R.id.fl_content, galleryGridFragment).commit();

        ((GalleryGridFragment) galleryGridFragment).setOnNavigationToGalleryListener(new GalleryGridFragment.onNavigationToGalleryListener() {
            @Override
            public void onNavigationGallery(ArrayList<GalleryImage> imagesList, int position) {
                GalleryActivity.navigation(GalleryGridActivity.this, imagesList, position);
            }
        });
    }

}
